<template>
  <a-drawer
    title="添加胰岛素记录"
    :maskClosable="true"
    :width="drawerWidth"
    placement="right"
    :closable="true"
    @close="handleCancel"
    :visible="visible"
    style="height: 100%; overflow: auto; padding-bottom: 53px"
  >
    <template slot="title">
      <div style="width: 100%">
        <span>{{ title }}</span>
        <span style="display: inline-block; width: calc(100% - 51px); padding-right: 10px; text-align: right">
          <a-button @click="toggleScreen" icon="appstore" style="height: 20px; width: 20px; border: 0px"></a-button>
        </span>
      </div>
    </template>

    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-form-model-item label="记录日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="recordTime">
          <a-date-picker
            style="width: 100%"
            placeholder="请选择记录日期"
            v-model="model.recordTime"
            :format="dateFormat"
            :disabled-date="disabledDate"
            @change="onDateChange"
            :getCalendarContainer="node => node.parentNode"
          />
        </a-form-model-item>

        <a-form-model-item label="注射时相" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="insulinType">
          <a-radio-group v-model="model.insulinType">
            <a-radio :value="1">胰岛素（早）</a-radio>
            <a-radio :value="2">胰岛素（午）</a-radio>
            <a-radio :value="3">胰岛素（晚）</a-radio>
            <a-radio :value="4">胰岛素（睡前）</a-radio>
            <a-radio :value="5">胰岛素泵（CSII）</a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item label="注射情况" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="injection">
          <a-radio-group v-model="model.injection">
            <a-radio :value="1"> 遵嘱注射 </a-radio>
            <a-radio :value="2"> 漏注射 </a-radio>
            <a-radio :value="3"> 自停打 </a-radio>
          </a-radio-group>
        </a-form-model-item>

        <a-form-model-item
          v-if="model.injection == 1"
          label="胰岛素用量"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="insulinDescribe"
        >
          <a-input-number
            style="width:200px;margin-right:10px"
            placeholder="请填写胰岛素用量"
            :min="0"
            v-model="model.insulinDescribe"
          />U
        </a-form-model-item>

        <a-form-model-item label="胰岛素图片" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="recordPhoto">
          <j-image-upload
            class="avatar-uploader"
            text="上传"
            v-model="model.recordPhoto"
            :is-multiple="true"
          ></j-image-upload>
        </a-form-model-item>

        <a-form-model-item label="测量备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="insulinRemark">
          <a-input placeholder="请填写测量备注" type="textarea" :maxLength="50" v-model="model.insulinRemark" />
        </a-form-model-item>
      </a-form-model>
    </a-spin>

    <div class="drawer-bootom-button" v-show="!disableSubmit">
      <a-popconfirm title="确定放弃编辑？" @confirm="handleCancel" okText="确定" cancelText="取消">
        <a-button style="margin-right: 0.8rem">取消</a-button>
      </a-popconfirm>
      <a-button @click="handleSubmit" type="primary" :loading="confirmLoading">提交</a-button>
    </div>
  </a-drawer>
</template>

<script>
import { PAGE_MENU_TYPE } from '@/store/mutation-types'
import { postAction } from '@/api/manage'
import moment from 'moment'

export default {
  name: 'InsulinModal',
  components: {},
  data() {
    return {
      drawerWidth: 700,
      disableSubmit: false,
      dateFormat: 'YYYY-MM-DD HH:mm:ss',
      title: '操作',
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      uploadLoading: false,
      confirmLoading: false,
      disabled: {},
      validatorRules: {
        recordTime: [{ required: true, message: '请选择测量时间!' }],
        injection: [{ required: true, message: '请选择注射情况!' }],
        insulinType: [{ required: true, message: '请选择测量项!' }],
        insulinDescribe: [{ validator: this.insulin }, { required: true }]
      }
    }
  },
  watch: {
    'model.injection'(n) {
      if (n != 1) {
        this.model.insulinDescribe = null
      }
      console.log(this.model.insulinDescribe)
    }
  },
  created() {},
  methods: {
    onDateChange(date, dateString) {
      this.model.recordTime = dateString
    },
    add(disabled) {
      this.visible = true
      this.disabled = disabled
      this.resetScreenSize()
    },
    disabledDate(current) {
      return current < moment(this.disabled.startTime) || current > moment(this.disabled.endTime)
    },
    // 根据屏幕变化,设置抽屉尺寸
    resetScreenSize() {
      let screenWidth = document.body.clientWidth
      if (screenWidth < 500) {
        this.drawerWidth = screenWidth
      } else {
        this.drawerWidth = 700
      }
    },
    //窗口最大化切换
    toggleScreen() {
      if (this.modaltoggleFlag) {
        this.modalWidth = window.innerWidth
      } else {
        this.modalWidth = 800
      }
      this.modaltoggleFlag = !this.modaltoggleFlag
    },
    insulin(rule, value, callback) {
      if (this.model.injection == 1 && !this.model.insulinDescribe) {
        callback(new Error('请填写胰岛素用量!'))
      } else {
        callback()
      }
    },
    handleSubmit() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          this.model.patientId = this.$route.query[PAGE_MENU_TYPE.PatientId]

          postAction('/101/08/1010803', this.model)
            .then(res => {
              if (res.success) {
                this.$message.success('添加成功')
                that.$emit('ok')
              } else {
                this.$message.warning(res.message)
              }
            })
            .finally(() => {
              this.confirmLoading = false
              this.handleCancel()
            })
        } else {
          return false
        }
      })
    },
    handleCancel() {
      this.visible = false
      this.disableSubmit = false
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style scoped>
.avatar-uploader > .ant-upload {
  width: 104px;
  height: 104px;
}
.ant-upload-select-picture-card i {
  font-size: 49px;
  color: #999;
}

.ant-upload-select-picture-card .ant-upload-text {
  margin-top: 8px;
  color: #666;
}

.ant-table-tbody .ant-table-row td {
  padding-top: 10px;
  padding-bottom: 10px;
}

.drawer-bootom-button {
  position: absolute;
  bottom: -8px;
  width: 100%;
  border-top: 1px solid #e8e8e8;
  padding: 10px 16px;
  text-align: right;
  left: 0;
  background: #fff;
  border-radius: 0 0 2px 2px;
}
</style>

